#ifndef CONNECTION_HPP
#define CONNECTION_HPP

#include <iostream>
#include <sys/epoll.h>
#include <cstdint>
#include <string>
#include <functional>
#include "InetAddr.hpp"

class Reactor; // 前向声明
class Channel;

// 普通fd处理的，管理一个Channel对象
using handler_t = std::function<std::string(std::string &)>;

class Connection
{
public:
    Connection() : _events(0), _owner(nullptr) {}

    void SetEvent(uint32_t event)
    {
        _events = event;
    }

    uint32_t GetEvent() const
    {
        return _events;
    }

    void SetOwner(Reactor *owner)
    {
        _owner = owner;
    }

    Reactor *GetOwner() const
    {
        return _owner;
    }

    void SetHandleRegeister(handler_t handler)
    {
        _handler = handler;
    }

    // 每个连接都有自己关心的事件，最常见的就是读和写了，定义处理读写事件的虚函数
    virtual void HandleRead() = 0;
    virtual void HandleWrite() = 0;
    virtual void HandleExcepter() = 0;
    virtual int GetSockFd() const = 0;
    ~Connection() {}

private:
    // 回指指针
    Reactor *_owner; // 连接所属的 Reactor

    // 每个连接需要关心的事件
    uint32_t _events; // 关心的事件

protected:
    handler_t _handler;
    // int _sockfd;
    // std::string _in_buffer;  // 读缓冲区
    // std::string _out_buffer; // 写缓冲区
};

#endif